﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WpfTestDemo.Core;
using WpfTestDemo.DBUtility;

namespace WpfTestDemo.Dal
{
    public class TestInfoDal
    {
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("Id", "TestInfo");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from TestInfo");
            strSql.Append(" where Id=@Id ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Id", MySqlDbType.Int32,11)         };
            parameters[0].Value = Id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(TestInfoCore model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TestInfo(");
            strSql.Append("`TestContent`)");
            strSql.Append(" values (");
            strSql.Append("@TestContent);");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@TestContent", MySqlDbType.VarChar,255)};
            parameters[0].Value = model.TestContent;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 增加一条数据--并返回当前数据的ID
        /// </summary>
        public int AddReturnId(TestInfoCore model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TestInfo(");
            strSql.Append("`TestContent`)");
            strSql.Append(" values (");
            strSql.Append("@TestContent);Select @@Identity;");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@TestContent", MySqlDbType.VarChar,255)};
            return DbHelperMySQL.ExecuteSqlReturnId(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TestInfoCore model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TestInfo set ");
            strSql.Append("TestContent=@TestContent");
            strSql.Append(" where Id=@Id ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@TestContent", MySqlDbType.VarChar,255),
                    new MySqlParameter("@Id", MySqlDbType.Int32,11)};
            parameters[0].Value = model.TestContent;
            parameters[1].Value = model.Id;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TestInfo ");
            strSql.Append(" where Id=@Id ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Id", MySqlDbType.Int32,11)         };
            parameters[0].Value = Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TestInfo ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TestInfoCore GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select `Id`,`TestContent` from TestInfo ");
            strSql.Append(" where Id=@Id ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Id", MySqlDbType.Int32,11)         };
            parameters[0].Value = Id;

            TestInfoCore model = new TestInfoCore();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TestInfoCore DataRowToModel(DataRow row)
        {
            TestInfoCore model = new TestInfoCore();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["TestContent"] != null && row["TestContent"].ToString() != "")
                {
                    model.TestContent = row["TestContent"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select `Id`,`TestContent`");
            strSql.Append(" FROM TestInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        public DataSet GetDataSQl(string strsql)
        {
            StringBuilder Sql = new StringBuilder();
            Sql.Append(strsql);
            return DbHelperMySQL.Query(strsql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM TestInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="pagIndex">当前页</param>
        /// <param name="pagSize">当前数据大小</param>
        /// <param name="operate">排序操作：asc顺序，desc 倒序</param>
        /// <param name="strWhere">查询条件: a.Id in('1','2','3')</param>
        /// <returns></returns>
        public DataSet GetListByPage(int pagIndex, int pagSize, OperateType operate = OperateType.ASC, string strWhere = "")
        {
            string _strWhere = string.Empty;
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                _strWhere = " WHERE  " + strWhere;

            }
            string strSql = string.Format("SELECT * FROM TestInfo a JOIN( select ID from TestInfo {0} {1} limit {2},{3}) b ON a.Id = b.ID", _strWhere, (" ORDER BY Id " + (operate.ToString() == "1" ? "asc" : "desc")), (pagIndex - 1) * pagSize, pagSize);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod

    }
}
